package com.sendi.system.util.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
 * 命令发送类
 * 调用 
 */
public class CommandSender {

	private static Runtime  runtime =null;
	 public static void main(String[] args) {
	/* CommandSender.Run("arp ", "-s 192.168.0.125  00-22-aa-7c-d4-21");
		while(true)
		{
			CommandResult a=	CommandSender.Run("arp -d  ", "");
			
			System.out.println(a.getResult()+".."+a.getResutlString());	
		}
		*/
		 CommandSender.Run("ping", "www.baidu.com"); 
		 
	} 
	public static CommandResult Run(String shell,String para)
	{
		CommandResult cr=new CommandResult();
		if(runtime==null)
		{
			runtime=Runtime.getRuntime();
		}
	 	   Process  process  =null;  

		   String  line=null;  

		   InputStream  is  =null;  

		   InputStreamReader  isr=null;  

		   BufferedReader  br  =null;  
		   StringBuffer sb=new StringBuffer("");
		 
		   try  

		   {  

		       process  =runtime.exec(shell+" "+para);  

		       is  =  process.getInputStream();  

		       isr=new  InputStreamReader(is);  

		       br  =new  BufferedReader(isr);  

		      

		       while(  (line  =  br.readLine())  !=  null  )  

		       {  
		    	   sb.append(line);
		    	 System.out.println(line);
		       }  
		       is.close();  

		       isr.close();  

		       br.close(); 
		       process.destroy();
		       cr.setResult(process.exitValue());
		       cr.setResutlString(sb.toString());
		    //   System.out.println(process.exitValue());
		       

		   }  

		   catch(Exception  e  )  

		   {  
			   System.out.println("excuting command error"+shell+" "+para+"/t"+e.getMessage());
			  // e.printStackTrace();
			  // runtime.exit(1);  
		   }  
		   if(shell.contains("ping")) //目前只针对linux平台
	       {
	    	   
	    		 String reg2=".*100%.*loss.*";
	    		 Pattern p = Pattern.compile(reg2);
	    		 Matcher m  =p.matcher(sb.toString());
	    	   if(m.matches())
	    	   {
	    		   cr.setResult(1);
	    	   }
	    	   else
	    	   {
	    		   cr.setResult(0);
	    	   }
	       }
	       
		   

	 return cr;
 	}
/*
 * breakstr结束字符串，遇到该字符结束循环
 */
	public static CommandResult Run(String shell,String para,String breakstr)
	{
		CommandResult cr=new CommandResult();
		Runtime  runtime  =  Runtime.getRuntime();  

		   Process  process  =null;  

		   String  line=null;  

		   InputStream  is  =null;  

		   InputStreamReader  isr=null;  

		   BufferedReader  br  =null;  
		   StringBuffer sb=new StringBuffer("");
		 
		   try  

		   {  

		       process  =runtime.exec(shell+" "+para);  

		       is  =  process.getInputStream();  

		       isr=new  InputStreamReader(is);  

		       br  =new  BufferedReader(isr);  

		      

		       while(  (line  =  br.readLine())  !=  null  )  

		       {  
		    	   sb.append(line);
		    	   if(line.contains("breakstr"))
		    	   {
		    		   break;
		    	   }
		    	   System.out.println(line);
		       }  
		       cr.setResult(process.exitValue());
		       cr.setResutlString(sb.toString());
		      

		       is.close();  

		       isr.close();  

		       br.close();  

		   }  

		   catch(Exception  e  )  

		   {  
			   e.printStackTrace();
			  // runtime.exit(1);  
		   }  
		   

	 return cr;
 	}
}
